package com.min.main.controller;

import com.min.common.entity.BaseController;
import com.min.common.entity.QueryPage;
import com.min.common.entity.R;
import com.min.main.entity.RoleEntity;
import com.min.main.service.RoleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色表
 *
 * @author fengt
 * @date 2021-06-23 11:41:08
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<RoleEntity, RoleService> {

    @Override
    public R one(@PathVariable("id") Long id){
        final RoleEntity role = service.getById(id);
        role.setMenuIdList(this.service.getMenus(role.getId()));
        return R.ok().put("data", role);
    }

    @Override
    public R add(@RequestBody RoleEntity t) {
        return this.service.saveOrUpdateWithMenus(t) ? R.ok() : R.error();
    }

    @Override
    public R edit(@RequestBody RoleEntity t) {
        return this.service.saveOrUpdateWithMenus(t) ? R.ok() : R.error();
    }

    @Override
    public R list(@RequestBody QueryPage<RoleEntity> page) {
        final QueryPage<RoleEntity> pages = service.lambdaQuery().setEntity(page.getQuery()).page(page);
        for (RoleEntity role : pages.getRecords()) {
            List<Long> menuIds = this.service.getMenus(role.getId());
            role.setMenuIdList(menuIds);
        }
        return R.ok().put("page", pages);
    }

    @Override
    public R remove(@PathVariable Long id){
        return this.service.removeWithMenus(id) ? R.ok() : R.error();
    }

    @Override
    public R remove(@RequestBody List<Long> ids) {
        return this.service.removeWithMenus(ids) ? R.ok() : R.error();
    }
}
